package com.example.lbstest.controller;

import com.example.lbstest.impl.ClassMapperServiceImpl;
import com.example.lbstest.pojo.Classes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {
    @Autowired
    ClassMapperServiceImpl service;

    @RequestMapping("/selectClasses")
    @ResponseBody
    public List<Classes> selectClasses(@RequestParam("number")String number) {

        System.out.println(number);
        List<Classes> list = service.selectClasses(number);
        System.out.println(list);
        if (list.size() != 0) {
            System.out.println("----显示课程-----");
        }
        return list;
    }
    @RequestMapping("/createClass")
    @ResponseBody
    public Classes createClass(@RequestParam("name")String name,
                               @RequestParam("teacher")String teacher,
                               @RequestParam("time")String time,
                               @RequestParam("color")String color,
                               @RequestParam("teacher_number")String teacher_number) {
        Classes classes = new Classes();
        String id = getUId();
        while(service.exitSameId(id) != null){
            id = getUId();
            System.out.println("same");
        }
        classes.setName(name);
        classes.setColor(color);
        classes.setTeacher(teacher);
        classes.setTime(time);
        classes.setNumber(0);
        classes.setId(id);
        classes.setTeacher_number(teacher_number);
        service.createClass(classes);
        System.out.println("----创建课程成功-----");
        return classes;
    }
    @RequestMapping("/getOneClass")
    @ResponseBody
    public Classes exitSameId(@RequestParam("id")String id){
        System.out.println("----课程查询成功-----");
        return service.exitSameId(id);
    }

    @RequestMapping("/addClass")
    @ResponseBody
    public String addClass(@RequestParam("id")String id,
                           @RequestParam("number")String number){
        int rr = service.addClass(id,number,getDate());
        int ar = service.addOneNumber(id);
        System.out.println("haha:"+rr);
        System.out.println("----加入课程成功-----");
        if(rr == 1 && ar == 1){
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/selectOneClass")
    @ResponseBody
    public Classes selectOneClass(@RequestParam("id")String id){
        System.out.println("----获取课程成功-----");
        return service.selectOneClass(id);
    }

    @RequestMapping("/selectClassListTeacher")
    @ResponseBody
    public List<Classes> selectClassListTeacher(@RequestParam("teacher_number")String teacher_number){
        List<Classes> list = service.selectClassListTeacher(teacher_number);
        System.out.println(list);
        if (list.size() != 0) {
            System.out.println("----显示课程-----");
        }
        return list;
    }


    private String getUId() {
        long time = System.currentTimeMillis();
        String s = String.valueOf(time);
        s=s.substring(5,13);
        long id = Long.valueOf(s);
        return String.valueOf(id);
    }
    //获取当前时间
    private String getDate() {
        Date dNow = new Date( );
        SimpleDateFormat ft =
                new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return ft.format(dNow);

    }
}
